<?php
/**
 * Created by PhpStorm.
 * User: SW
 * Date: 2017/10/25
 * Time: 14:30
 */

function ajaxHeader()
{
    // 指定允许其他域名访问
    header('Access-Control-Allow-Origin:*');
// 响应类型
    header('Access-Control-Allow-Methods:POST');
// 响应头设置
    header('Access-Control-Allow-Headers:x-requested-with,content-type');
}


/**
 * 获取Http数据
 * @return array
 */
function getHttpData()
{
    return object_to_array(json_decode(file_get_contents("php://input")));
}


/**
 * 数组 转 对象
 * @param array $arr 数组
 * @return object
 */
function array_to_object($arr)
{
    if (gettype($arr) != 'array') {
        return;
    }
    foreach ($arr as $k => $v) {
        if (gettype($v) == 'array' || getType($v) == 'object') {
            $arr[$k] = (object)array_to_object($v);
        }
    }
    return (object)$arr;
}

/**
 * 对象 转 数组
 * @param object $obj 对象
 * @return array
 */
function object_to_array($obj)
{
    $obj = (array)$obj;
    foreach ($obj as $k => $v) {
        if (gettype($v) == 'resource') {
            return;
        }
        if (gettype($v) == 'object' || gettype($v) == 'array') {
            $obj[$k] = (array)object_to_array($v);
        }
    }

    return $obj;
}

/**
 * 回复状态
 * @param $err 错误码
 * @param $errMsg 错误信息
 */
function doResopnse($ecode,$emsg){
    echo json_encode(array('ecode'=>$ecode,'emsg'=>$emsg));
}

function responseOk(){
   doResopnse(0,"ok");
}